package com.tzy.evaluation.service;
import com.tzy.common.entity.Comments;
import com.tzy.evaluation.mapper.CommentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author 谭同学
 * @since 2022-03-18
 */
@Service
public class CommentService extends ServiceImpl<CommentMapper, Comments> implements ICommentService {

    @Resource
    private CommentMapper commentMapper;

    /**
     * 查询所有一级评论
     *
     * @param evaluationId
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    @Cacheable(cacheNames = "comments")
    public List<Map<String, Object>> getAllFirstLevelReviews(int evaluationId, Integer pageNum, Integer size) {
        return commentMapper.findAllFirstLevelReviews(evaluationId, pageNum, size);
    }

    /**
     * @param evaluationId
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    @Cacheable(cacheNames = "comments")
    public List<Map<String, Object>> userComment(int evaluationId, Integer pageNum, Integer size) {
        return commentMapper.userComment(evaluationId, pageNum, size);
    }

    /**
     * @param evaluationId
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    @Cacheable(cacheNames = "comments")
    public List<Map<String, Object>> replyUser(int replyId,int evaluationId, Integer pageNum, Integer size) {
        return commentMapper.replyUser(replyId,evaluationId, pageNum, size);
    }

    /**
     * @param id
     * @param evaluationId
     * @return
     */
    @Override
    @Cacheable(cacheNames = "comments")
    public int commentCount(Integer id, Integer evaluationId) {
        return commentMapper.commentCount(id,evaluationId);
    }

    /**
     * @param evaluationId
     * @return
     */
    @Override
    @Cacheable(cacheNames = "comments")
    public int commentCounts(Integer evaluationId) {
        return commentMapper.commentCounts(evaluationId);
    }

    /**
     * 插入一条记录（选择字段，策略插入）
     *
     * @param entity 实体对象
     */
    @Override
    @CacheEvict(value = {"comments","list"},allEntries = true)
    public boolean save(Comments entity) {
        return super.save(entity);
    }
    /**
     * 根据 ID 选择修改
     *
     * @param entity 实体对象
     */
    @Override
    @CacheEvict(value = {"comments","list"},allEntries = true)
    public boolean updateById(Comments entity) {
        return super.updateById(entity);
    }
}
